package com.ww.learn.coroutine.context

import kotlinx.coroutines.*

class Acitvity {
    private val mainScope = CoroutineScope(Dispatchers.Default)

    fun destroy() {
        mainScope.cancel()
    }

    fun doSomething() {
        repeat(10) { i ->
            mainScope.launch {
                delay((i + 1) * 200L)
                println("Coroutine $i is done")
            }
        }
    }
}

// 打印结果
//启动协程
//Coroutine 0 is done
//Coroutine 1 is done
//结束协程
fun main() = runBlocking {
    val acitvity = Acitvity()
    acitvity.doSomething() // 启动协程
    println("启动协程")
    delay(500L) // 延迟半秒
    println("结束协程")
    acitvity.destroy()
    delay(1000L)
}